package com.crrc.dataparse.parse.TemplateAlg;

import com.crrc.dataparse.entity.*;
import com.crrc.dataparse.parse.ITemplateParser;

import java.util.List;

/*
    协议体处理方案
    牵引多频率参数包使用
 */
public class MFTemp implements ITemplateParser {
    private static final int BASE_INFO_PARAMETER = 1;
    private static final int WORK_STATUS_PARAMETER = 2;
    private static final int SUBTEMPLATE_PARAMETER = 3;
    private static final int NULL_PARAMETER = 4;
    private static final int VIRTUAL_PARAMETER = 5;
    private static final int Static_PARAMETER = 6;
    private static final int BASE_INFO_PARAMETER_Line = 7;
    private static final int BASE_INFO_PARAMETER_Car = 8;
    private static final int BASE_INFO_PARAMETER_Terminal = 9;
    private static final int BASE_INFO_PARAMETER_Time = 10;
    private static final int BASE_INFO_PARAMATER_TrainType = 11;
    private int interval = 1000;//状态数据和预充电数据，每包的时间间隔为100ms

    public MFTemp() {
    }

    /**
     *
     * @param packetData: 原始数据
     * @param offset: 当前解析到的偏移量
     * @param template: 模板
     * @param paserInfo: 处理方案信息
     * @param pdp: 解析后数据包
     * @return
     * @author MengXiangTong
     * @date 2021/11/9 22:13
     */
    @Override
    public int templateParse(String packetData, int offset, Template template, PaserInfo paserInfo, ParsedResult pdp) throws Exception {
        List<Tem2TemPara> tempList = template.getTem2TemParaList();
        TemplatePara tempPara;
        //遍历所有模板
        for(int i = 0; i < tempList.size(); i++) {
//            if(offset + 16 >= packetData.length()){
//                offset = -1;
//                break;
//            }
            tempPara = tempList.get(i).getTemplatePara();
            int paranum = tempPara.getParaNum();
            offset = getPara2(tempPara, packetData, pdp, offset, paranum);
        }
        return offset;
    }


    /**
     *
     * @param tempPara: 模板参数
     * @param packetData: 原始数据
     * @param pdp: 解析后数据
     * @param offset: 当前解析到的偏移
     * @param num: 参数个数
     * @return 解析后的偏移量
     * @author MengXiangTong
     * @date 2021/11/9 22:08
     */
    public int getPara2(TemplatePara tempPara, String packetData, ParsedResult pdp, int offset, int num) throws Exception {
        int paraType = tempPara.getParameterType();
        int length = tempPara.getLength();

        if (paraType == NULL_PARAMETER && num != 0) {
            for (int i = 0; i < num && offset + length <= packetData.length(); ++i){
            offset += length;
            }
        }
        if (paraType != SUBTEMPLATE_PARAMETER) {
//            int length = tempPara.getLength();
            //int i;
            //String tempParaValue;
            if (paraType == BASE_INFO_PARAMETER) {
                for(int i = 0; i < num; ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            } else if (paraType == WORK_STATUS_PARAMETER && num != 0) {
                Long time = interval  / (long)num;

                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addWorkStatus(tempPara.getParameterID(), (long)i, tempParaValue);
                    offset += length;
                }
            } else if (paraType == BASE_INFO_PARAMETER_Line) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                    pdp.setlineNo(tempPara.getParameterID());
                }
            } else if (paraType == BASE_INFO_PARAMETER_Car) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                    pdp.setmetroNo(tempPara.getParameterID());
                }
            } else if (paraType == BASE_INFO_PARAMETER_Terminal) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            } else if (paraType == BASE_INFO_PARAMETER_Time) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            } else if (paraType == BASE_INFO_PARAMATER_TrainType) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            }
        }
        return offset;
    }
}
